package com.ruoyi.web.controller.data;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.CollectionTask;
import com.ruoyi.system.service.ICollectionTaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 采集任务信息Controller
 *
 * @author ruoyi
 * @date 2025-04-30
 */
@Controller
@RequestMapping("/system/statistics")
public class StatisticsController extends BaseController
{
    private String prefix = "system/statistics";
//
    @RequiresPermissions("system:statistics:view")
    @GetMapping()
    public String task()
    {
        return prefix + "/statistics";
    }

    @RequiresPermissions("system:statistics:echart_1")
    @GetMapping("/echart_1")
    @ResponseBody
    public AjaxResult getEchart1Data()
    {
        // 获取当前时间并计算时间点
        java.util.Date now = new java.util.Date();
        int totalMinutes = now.getHours() * 60 + now.getMinutes();
        int totalIntervals = (int) Math.floor(totalMinutes / 10.0) + 1;

        // 生成时间点和测试数据
        java.util.List<String> times = new java.util.ArrayList<>();
        java.util.List<Integer> data = new java.util.ArrayList<>();
        java.util.Random random = new java.util.Random();

        for (int i = 0; i < totalIntervals; i++) {
            int hour = (int) Math.floor(i * 10 / 60.0);
            int minute = (i * 10) % 60;
            times.add(String.format("%02d:%02d", hour, minute));
            data.add(random.nextInt(51) + 50); // 生成50-100之间的随机数
        }

        // 构建返回数据
        java.util.Map<String, Object> result = new java.util.HashMap<>();
        result.put("times", times);
        result.put("data", data);

        return AjaxResult.success(result);
    }

    @RequiresPermissions("system:statistics:echart_2")
    @GetMapping("/echart_2")
    @ResponseBody
    public AjaxResult getEchart2Data()
    {
        // 获取当前时间并计算半小时间隔的时间点
        java.util.Date now = new java.util.Date();
        int totalMinutes = now.getHours() * 60 + now.getMinutes();
        int totalIntervals = (int) Math.floor(totalMinutes / 30.0) + 1;

        // 生成时间点和测试数据
        java.util.List<String> times = new java.util.ArrayList<>();
        java.util.List<Double> sales = new java.util.ArrayList<>();
        java.util.List<Double> orders = new java.util.ArrayList<>();
        java.util.Random random = new java.util.Random();

        for (int i = 0; i < totalIntervals; i++) {
            int hour = (int) Math.floor(i * 30 / 60.0);
            int minute = (i * 30) % 60;
            times.add(String.format("%02d:%02d", hour, minute));

            // 生成销售额数据（4000-6000之间的随机数）
            sales.add(4000 + random.nextDouble() * 2000);

            // 生成订单数据（3000-4500之间的随机数）
            orders.add(3000 + random.nextDouble() * 1500);
        }

        // 构建返回数据
        java.util.Map<String, Object> result = new java.util.HashMap<>();
        result.put("times", times);
        result.put("sales", sales);
        result.put("orders", orders);

        return AjaxResult.success(result);
    }

    /**
     * 获取快递公司当天快递量数据
     */
    @GetMapping("/echart_3")
    @ResponseBody
    public AjaxResult getEchart3Data() {
        // 生成当天的时间点（每20分钟一个点）
        List<String> times = new ArrayList<>();
        List<Integer> sfData = new ArrayList<>();
        List<Integer> emsData = new ArrayList<>();
        List<Integer> bestData = new ArrayList<>();
        List<Integer> ytoData = new ArrayList<>();
        List<Integer> ztoData = new ArrayList<>();
        List<Integer> stoData = new ArrayList<>();
        List<Integer> yundaData = new ArrayList<>();

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startTime = now.withHour(0).withMinute(0).withSecond(0);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");

        Random random = new Random();
        for (int i = 0; i <= 72; i++) { // 24小时 * 3 (20分钟一个点)
            LocalDateTime pointTime = startTime.plusMinutes(i * 20);
            if (pointTime.isAfter(now)) {
                break;
            }
            times.add(pointTime.format(formatter));

            // 生成模拟数据
            sfData.add(random.nextInt(100) + 200);    // 顺丰快递
            emsData.add(random.nextInt(80) + 150);    // 邮政速递
            bestData.add(random.nextInt(50) + 100);   // 百世快递
            ytoData.add(random.nextInt(120) + 180);   // 圆通速递
            ztoData.add(random.nextInt(150) + 250);   // 中通速递
            stoData.add(random.nextInt(90) + 160);    // 申通快递
            yundaData.add(random.nextInt(100) + 190); // 韵达快递
        }

        Map<String, Object> data = new HashMap<>();
        data.put("times", times);
        data.put("sf", sfData);
        data.put("ems", emsData);
        data.put("best", bestData);
        data.put("yto", ytoData);
        data.put("zto", ztoData);
        data.put("sto", stoData);
        data.put("yunda", yundaData);

        return AjaxResult.success(data);
    }
}
